#include "CanTest.h"
#include "debug_prt.h"

can_trasnmit_message_struct Tx1Message;
can_receive_message_struct Rx1Message;

can_trasnmit_message_struct Tx2Message;
can_receive_message_struct Rx2Message;



void gd_can_init(uint32_t can)
{
    uint32_t can_id = 0U;
    if(CAN0 == can){
        can_id = 0U;
    }else{
        can_id = 1U;
    }

		rcu_periph_clock_enable(RCU_AF);
		
		if (can_id == 0U)
		{
			rcu_periph_clock_enable(RCU_GPIOA);
			rcu_periph_clock_enable(RCU_CAN0);
			gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12);
			gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_11);
			
			//RS
			rcu_periph_clock_enable(RCU_GPIOB); 
			rcu_periph_clock_enable(RCU_AF); 
			gpio_pin_remap_config(GPIO_SWJ_SWDPENABLE_REMAP,ENABLE);
			gpio_init(GPIOB, GPIO_MODE_OUT_OD, GPIO_OSPEED_2MHZ, GPIO_PIN_4);
			
			gpio_bit_reset(GPIOB,GPIO_PIN_4);	//0 work, 1 standby
		}
		else
		{
			rcu_periph_clock_enable(RCU_GPIOB);
			rcu_periph_clock_enable(RCU_CAN1);
			gpio_pin_remap_config(GPIO_CAN1_REMAP,ENABLE);
			
			gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
			gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_5);
			
			
			
			//RS
			rcu_periph_clock_enable(RCU_GPIOB);
			gpio_init(GPIOB, GPIO_MODE_OUT_OD, GPIO_OSPEED_2MHZ, GPIO_PIN_7);
			gpio_bit_reset(GPIOB,GPIO_PIN_7);	//0 work, 1 standby
		}
			


		
}


void can_config(uint32_t can,uint32_t baudrate)
{
    can_parameter_struct can_parameter;
     
    can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
    /* initialize CAN register */
		if(CAN0 == can)
		{	
			can_deinit(CAN0);
		}else
		{
			can_deinit(CAN1);
    }
    /* initialize CAN parameters */
    can_parameter.time_triggered = DISABLE;
    can_parameter.auto_bus_off_recovery = DISABLE;
    can_parameter.auto_wake_up = DISABLE;
    can_parameter.auto_retrans = DISABLE;
    can_parameter.rec_fifo_overwrite = DISABLE;
    can_parameter.trans_fifo_order = DISABLE;
    can_parameter.working_mode = CAN_NORMAL_MODE;  
    /* initialize CAN */
		if(CAN0 == can)
		{
    can_init(CAN0, &can_parameter);
		}else{
    can_init(CAN1, &can_parameter);
    }
    /* config CAN0 baud rate */
		if(CAN0 == can)
		{
			can_frequency_set(CAN0, baudrate);
		}else{
    /* config CAN1 baud rate */
			can_frequency_set(CAN1, baudrate);
    }
		
		/* initialize filter */ 
    //can1_filter_start_bank(14);
    //can_filter_mask_mode_init(DEV_CAN0_ID, DEV_CAN0_MASK, CAN_EXTENDED_FIFO0, 0);
    //can_filter_mask_mode_init(DEV_CAN1_ID, DEV_CAN1_MASK, CAN_EXTENDED_FIFO1, 15);
		

//		if(CAN0 == can)
//		{
//			initCANfilter(0);
//		}else{
//			initCANfilter(1);
//    } 
		
    /* configure CAN0 NVIC */
		if(CAN0 == can)
		{
			nvic_irq_enable(CAN0_RX0_IRQn, 0, 0);
			/* enable can receive FIFO0 not empty interrupt */
			can_interrupt_enable(CAN0, CAN_INTEN_RFNEIE0);
		}
    /* configure CAN1 NVIC */
		else
		{
			nvic_irq_enable(CAN1_RX1_IRQn, 1, 0);		
			can_interrupt_enable(CAN1, CAN_INTEN_RFNEIE1);
		}

}

  

void CAN0_TX_IRQHandler(void)
{
}

void CAN0_RX0_IRQHandler(void)
{
	//if( can_interrupt_flag_get( CAN0, CAN_INT_FLAG_RFF0 ) != RESET )
	{
		can_message_receive(CAN0, CAN_FIFO0, &Rx1Message);
		printf("CAN0_RX0_IRQHandler %02x\r\n", Rx1Message.rx_data[0]);
	}
	
}


void CAN0_RX1_IRQHandler(void)
{
	//if( can_interrupt_flag_get( CAN0, CAN_INT_FLAG_RFF1 ) != RESET )
	{
		can_message_receive(CAN0, CAN_FIFO0, &Rx1Message);
		printf("CAN0_RX1_IRQHandler %02x\r\n", Rx1Message.rx_data[0]);
	}
}


void CAN0_EWMC_IRQHandler(void)
{
	
}


void CAN1_TX_IRQHandler(void)
{
}

void CAN1_RX0_IRQHandler(void)
{
	//if( can_interrupt_flag_get( CAN1, CAN_INT_FLAG_RFF0 ) != RESET )
	{
		can_message_receive(CAN1, CAN_FIFO1, &Rx2Message);
		printf("CAN1_RX0_IRQHandler %02x\r\n", Rx2Message.rx_data[0]);
	}
}

void CAN1_RX1_IRQHandler(void)
{
	//if( can_interrupt_flag_get( CAN1, CAN_INT_FLAG_RFF1 ) != RESET )
	{
		can_message_receive(CAN1, CAN_FIFO1, &Rx2Message);
		printf("CAN1_RX1_IRQHandler %02x\r\n", Rx2Message.rx_data[0]);
	}
}

void CAN1_EWMC_IRQHandler(void)
{
}



void initCAN(void)
{
	uint8_t transmit_mailbox;
	uint32_t timeout = 0xFFFF;
	
	/*configure CAN*/
	  gd_can_init(CAN0);
		gd_can_init(CAN1);
	
		/*configure CAN filter*/
		can_config(CAN0,500000);
		can_config(CAN1,500000);
	
	
	can_struct_para_init(CAN_RX_MESSAGE_STRUCT, &Rx2Message);
	can_struct_para_init(CAN_RX_MESSAGE_STRUCT, &Rx1Message);
	
	can_struct_para_init(CAN_RX_MESSAGE_STRUCT, &Tx2Message);
	can_struct_para_init(CAN_RX_MESSAGE_STRUCT, &Tx1Message);
	
	
	Tx1Message.tx_sfid = 0x01;
	Tx1Message.tx_efid = 0x02;
	Tx1Message.tx_data[0] = 0xA1;
	Tx1Message.tx_dlen = 1;
	Tx1Message.tx_ft = CAN_FT_DATA;
	Tx1Message.tx_ff = CAN_FF_STANDARD;
	
	
	
	Tx2Message.tx_sfid = 0x03;
	Tx2Message.tx_efid = 0x04;
	Tx2Message.tx_data[0] = 0xB1;
	Tx2Message.tx_dlen = 1;
	Tx2Message.tx_ft = CAN_FT_DATA;
	Tx2Message.tx_ff = CAN_FF_STANDARD;
	
	transmit_mailbox = can_message_transmit(CAN0, &Tx1Message);
	while ((CAN_TRANSMIT_OK != can_transmit_states(CAN0, transmit_mailbox)) && (0 != timeout))
	{
		timeout--;
  }
	
	
	//can_message_transmit(CAN1, &Tx2Message);
	
	
	while(1)
	{
		if (timeout == 0x01)
		{
			timeout = 0x01;
		}
	}
	
}
















